home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The 640 MEG Shareware Studio 2
/
The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO
/
basic
/
iqb9104.zip
/
LIST.BAS
< prev
next >
Wrap
BASIC Source File
|
1991-03-20
|
6KB
|
220 lines
' LIST.BAS, demonstrates using a random disk file
' to maintain a linked list.
DECLARE SUB Delete ()
DECLARE SUB DisplayData ()
DECLARE FUNCTION Menu$ ()
DECLARE SUB EnterData ()
DECLARE SUB InsertRecord ()
ON ERROR GOTO ErrorHandler
DEFINT A-Z
TYPE PartRecord
Named AS STRING * 16
Number AS STRING * 10
NextPart AS INTEGER
END TYPE
DIM SHARED Part AS PartRecord
DIM SHARED OldPart1 AS PartRecord
DIM SHARED OldPart2 AS PartRecord
' Try to open header file. If file not found,
' we must be starting a new list, and the
' error routine sets FirstRecord to 1.
100 OPEN "partlist.hdr" FOR INPUT AS #1
INPUT #1, FirstRecord, NumberOfRecords
CLOSE #1
200 ' Open data file, point NextRecord to
' the end of the file.
OPEN "partlist.dat" FOR RANDOM AS #1 _
LEN = LEN(Part)
NextRecord = LOF(1) \ LEN(Part) + 1
CLS
DO
SELECT CASE Menu$
CASE "E"
CALL EnterData
CASE "X"
' Write value of FirstRecord to file
OPEN "partlist.hdr" FOR OUTPUT AS #2
WRITE #2, FirstRecord, NumberOfRecords
CLOSE : CLS : END
CASE "P"
CALL DisplayData
CASE "D"
CALL Delete
END SELECT
LOOP
END ' end of LIST.BAS
ErrorHandler:
IF ERR = 53 AND ERL = 100 THEN
FirstRecord = 1: RESUME 200
END IF
PRINT "Error"; ERR
PRINT "Hit any key to exit program."
WHILE INKEY$ = "": WEND
END
' ***********************************************
SUB Delete
' Delete an element from the linked list.
SHARED NextRecord, FirstRecord, NumberOfRecords
DIM Target AS STRING * 16
NumDeleted = 0
CLS
INPUT "Enter name of part to delete:"; Target
PRINT
j = FirstRecord
DO
GET #1, j, part
IF Part.Named = Target THEN
Part.Named = "????????????????"
NextOne = Part.NextPart
NumDeleted = 1
NumberOfRecords = NumberOfRecords - 1
IF j = FirstRecord THEN
FirstRecord = Part.NextPart
PUT #1, j, Part
ELSE
PUT #1, j, Part
GET #1, LastRecord, Part
Part.NextPart = NextOne
PUT #1, LastRecord, Part
END IF
EXIT DO
ELSE
LastRecord = j
j = Part.NextPart
IF j = 0 THEN EXIT DO
END IF
LOOP
IF NumDeleted = 0 THEN
PRINT "Not found"
ELSE
PRINT "Found and deleted."
END IF
PRINT : PRINT "Press any key"
WHILE INKEY$ = "": WEND
END SUB ' end of delete
' ***********************************************
SUB DisplayData
' Displays the list elements on the screen.
SHARED NextRecord, FirstRecord, NumberOfRecords
CLS
j = FirstRecord
FOR i = 1 TO NumberOfRecords
GET #1, j, part
PRINT "Record #"; i; ": ";
PRINT Part.Named, Part.Number
j = Part.NextPart
NEXT i
WHILE INKEY$ = ""
WEND
END SUB ' end of DisplayData
' ***********************************************
SUB EnterData
' Enter a new record into the linked list.
SHARED NumberOfRecords
CLS
DO
INPUT "Enter part name (blank to exit): ", Temp$
IF Temp$ = "" THEN
EXIT DO
ELSE
Part.Named = Temp$
END IF
INPUT "Enter part number: ", Part.Number
NumberOfRecords = NumberOfRecords + 1
' If NextRecord = 1 we are starting a new
' data file. Otherwise we must inset the new
' record into its proper position in the
' existing list.
IF NextRecord = 1 THEN
part.NextPart = 0
PUT #1, NextRecord, part
NextRecord = NextRecord + 1
ELSE
CALL InsertRecord
END IF
LOOP
END SUB
' ***********************************************
SUB InsertRecord
' Inserts a new record into its proper position
' in random file linked list.
SHARED NextRecord, FirstRecord, NumberOfRecords
i = FirstRecord
DO
' Retrieve the first record in the list.
GET #1, i, OldPart2
' Should the new entry come before it?
IF UCASE$(Part.Named) < UCASE$(OldPart2.Named) _
THEN
' If i = FirstRecord then the new record will
' become the first one in the list.
IF i = FirstRecord THEN
Part.NextPart = FirstRecord
FirstRecord = NextRecord
PUT #1, NextRecord, Part
NextRecord = NextRecord + 1
EXIT DO
' If i <> FirstRecord, we must put the new
' record between two existing records.
ELSE
Part.NextPart = OldPart1.NextPart
OldPart1.NextPart = NextRecord
PUT #1, LastRecord, OldPart1
PUT #1, NextRecord, Part
NextRecord = NextRecord + 1
EXIT DO
END IF
ELSE
' If we haven't reached the end of the file,
' save the current record then loop back
' to get the next one.
IF OldPart2.NextPart > 0 THEN
LastRecord = i
i = OldPart2.NextPart
OldPart1 = OldPart2
' If we have reached the end of the file,
' the new record must be "larger" than all
' existing records and should therefore
' become the last record in the list.
ELSE
OldPart2.NextPart = NextRecord
PUT #1, i, OldPart2
Part.NextPart = 0
PUT #1, NextRecord, Part
NextRecord = NextRecord + 1
EXIT DO
END IF
END IF
LOOP
END SUB 'end of InsertRecord
' ***********************************************
FUNCTION Menu$
' Displays a Menu and returns the key pressed.
CLS
LOCATE 5, 10
PRINT "Linked list demonstration program"
LOCATE 6, 10
PRINT "------------------------------------"
LOCATE 7, 12
PRINT "E -> Enter new data"
LOCATE 8, 12
PRINT "D -> Delete old data"
LOCATE 9, 12
PRINT "P -> Print data on screen"
LOCATE 11, 12
PRINT "X -> Exit program"
DO: k$ = INKEY$: LOOP UNTIL k$ <> ""
Menu$ = UCASE$(k$)
END FUNCTION